package com.wisezone.java1902.d12_conections;

import java.util.Stack;

/**
 * 栈结构: 先进后出
 * */
public class MyStack<E> implements IStack<E>{
    private Object[] stacks;
    private int initCapacity = 5;// 初始容量 
    private int index = 0; //下标
    public MyStack(){
    	stacks = new Object[initCapacity];
    }
	/**
	 * 压栈
	 * @param item
	 */
	public void push(E item) {
		if(index >= initCapacity) {
			System.out.println("栈已满");
			return;
		}
		stacks[index++] = item;
	}
	
	/**
	 * 弹栈
	 * @throws MyException 
	 */
	public E pop() throws MyException {
		if(index <= 0) {
//			System.out.println("栈已空");
			throw new MyException("栈已空");
		}
		return (E) stacks[--index];
	}
	
	/**
	 * 获取元素个数
	 */
	public int size() {
		return index;
	}
	
	public static void main(String[] args) throws MyException {
		IStack<String> statcks = new MyStack<String>();
//		Stack<String> statcks = new Stack<String>();
		statcks.push("主界面");
		statcks.push("列表界面");
		statcks.push("详细页面");

		int size = statcks.size();
		for (int i = 0; i < size; i++) {
			String item = statcks.pop();
			System.out.println(item);			
		}
		String item = statcks.pop();
		System.out.println(item);	
	}
}
